package com.diancan.modules.admin.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diancan.common.Result;
import com.diancan.domain.User;
import com.diancan.modules.user.user.UserService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("adminUser")
@CrossOrigin
public class AdminUserController {
    private final UserService userService;

    public AdminUserController(UserService userService) {
        this.userService = userService;
    }

    // 获取全部用户
    @GetMapping("getUser")
    public Result getUser(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("created");
        return Result.buildSuccessResult(userService.list(wrapper));
    }

    // 修改
    @PostMapping("update")
    public Result update(@RequestBody User user){
        if (user.getId() == null){
            return Result.buildFailureResult("userId为空");
        }
        return Result.buildSuccessResult(userService.updateById(user));
    }

    // 删除
    @GetMapping("delete")
    public Result delete(String id){
        if (id == null){
            return Result.buildFailureResult("id为空");
        }
        return Result.buildSuccessResult(userService.removeById(id));
    }

    // 查找用户
    @GetMapping("search")
    public Result search(String key){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("id",key).or().like("phone",key).orderByDesc("created");
        return Result.buildSuccessResult(userService.list(wrapper));
    }
}
